/**************************************************************
 *@ProjectName: blweibo
 *@FileName: SearchActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.zhiweibo.models.Topic;
import com.zhiweibo.models.User;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class SearchActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener, OnItemClickListener {

    /** The search flag. */
    private int searchFlag = -1;

    /** The keyword. */
    private String keyword = null;

    /** The m list view. */
    private ListView mListView;

    /** The m list. */
    private List mList = new ArrayList();

    /** The adapter. */
    private ListAdapter adapter;

    /** The m page. */
    private int mPage = 1;

    /** The m back list. */
    private List mBackList = new ArrayList();

    /**
     * Load more.
     */
    private void loadMore() {
        mPage += 1;
        reloadBegine(0);
        new FetchDate().execute(0);
    }

    /**
     * Reload.
     */
    private void reload() {
        reloadBegine(1);
        mListView.setSelection(0);
        mBackList.clear();
        mPage = 1;
        new FetchDate().execute(0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        case 1:
            finish();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.listbase);
        setTitleBar(1, getString(R.string.imageviewer_back), getString(R.string.menu_search), null);
        searchFlag = getIntent().getIntExtra("searchFlag", 1);
        keyword = getIntent().getStringExtra("keyword");
        mListView = (ListView) findViewById(R.id.lvUser);
        mListView.setOnItemClickListener(this);
        adapter = new ListAdapter();
        mListView.setAdapter(adapter);
        reload();
    }

    /**
     * The Class ListAdapter.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class ListAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            if (mList == null || mList.size() == 0) {
                return 1;
            } else {
                return mList.size() + 2;
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (mList == null || mList.size() == 0) {
                return mReloadItem;
            } else {
                position--;
                if (position == -1) {
                    return mReloadItem;
                } else if (position == mList.size()) {
                    return mLoadmoreItem;
                } else {
                    try {
                        switch (searchFlag) {
                        case 1:
                            if (convertView == null || !(convertView instanceof MBlogListItemView)) {
                                return new MBlogListItemView(SearchActivity.this, (Topic) mList.get(position));
                            } else {
                                ((MBlogListItemView) convertView).update(SearchActivity.this,
                                        (Topic) mList.get(position));
                                return convertView;
                            }
                        case 0:
                            if (convertView == null || !(convertView instanceof UserListItemView)) {
                                return new UserListItemView(SearchActivity.this, (User) mList.get(position));
                            } else {
                                ((UserListItemView) convertView)
                                        .update(SearchActivity.this, (User) mList.get(position));
                                return convertView;
                            }
                        }
                    } catch (Exception e) {
                        return null;
                    }
                    return null;
                }
            }
        }
    }

    /**
     * The Class FetchDate.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FetchDate extends AsyncTask<Object, Integer, Object> {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            switch (searchFlag) {
            case 1:
                mList = HttpUtil.getTopics(SearchActivity.this, Constant.ST + "?keyword=" + URLEncoder.encode(keyword)
                        + "&count=50&page=" + mPage);
                return mList;
            case 0:
                mList = HttpUtil.getUsers(SearchActivity.this, Constant.SM + "?keyword=" + URLEncoder.encode(keyword)
                        + "&count=50&page=" + mPage);
                return mList;
            }
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            switch (searchFlag) {
            case 1:
                for (int i = 0; i < mList.size(); i++) {
                    boolean b1 = true;
                    Topic t1 = (Topic) mList.get(i);
                    for (int j = 0; j < mBackList.size(); j++) {
                        Topic t2 = (Topic) mBackList.get(j);
                        if (t1.getTid().equals(t2.getTid())) {
                            b1 = false;
                            break;
                        }
                    }
                    if (b1) {
                        mBackList.add(t1);
                    }
                }
                mList.clear();
                mList.addAll(mBackList);
                break;
            case 0:
                for (int i = 0; i < mList.size(); i++) {
                    boolean b1 = true;
                    User t1 = (User) mList.get(i);
                    for (int j = 0; j < mBackList.size(); j++) {
                        User t2 = (User) mBackList.get(j);
                        if (t1.getUid().equals(t2.getUid())) {
                            b1 = false;
                            break;
                        }
                    }
                    if (b1) {
                        mBackList.add(t1);
                    }
                }
                mList.clear();
                mList.addAll(mBackList);
            }
            adapter.notifyDataSetChanged();
            reloadEnd();
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
     * .AdapterView, android.view.View, int, long)
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (view == mReloadItem) {
            reload();
        } else if (view == mLoadmoreItem) {
            loadMore();
        } else {
            position--;
            switch (searchFlag) {
            case 1:
                startActivity(new Intent(SearchActivity.this, DetailWeiboActivity.class).putExtra("topic",
                        (Topic) mList.get(position)));
                break;
            case 0:
                startActivity(new Intent(SearchActivity.this, UserInfoActivity2.class).putExtra("user",
                        (User) mList.get(position)));
                break;
            }
        }
    }
}
